{% extends "base.html" %}
{% load i18n %}
{% load static %}
{% load icons %}

{% block content %}
<div class="page-wrapper">
    {% include 'pleasewaitdialog.html' %}
    <!--block content-->
    <div class="page-header d-print-none">
        <div class="container-xl">
            <div class="row g-2 align-items-center">
                <div class="col">
                    <div class="page-pretitle">
                        Storage Pools
                    </div>
                    <h2 class="page-title">
                        {% trans "Storage Pools" %}
                    </h2>
                </div>
            </div>
        </div>
    </div>
    <div class="page-body">
        <div class="container-xl">
            <div class="card">
                <div class="card-header">
                    <h2>{{ pool }}</h2>
                    <div class="ribbon {% if status %}bg-teal{% else %}bg-red{% endif %}">{{ type|upper }}</div>
                </div>
                <div class="card-body">
                    <p class="text-muted">
                        <div class="row">
                            <div class="col-lg-4">
                                <dl class="row">
                                    <dt class="col-3">{% trans "Pool name" %}:</dt>
                                    <dd class="col-9">{{ pool }}</dd>
                                    <dt class="col-3">{% trans "Pool type" %}:</dt>
                                    <dd class="col-9">{% if not type %}{% trans "None" %}{% else %}{{ type }}{% endif %}</dd>
                                    <dt class="col-3">{% trans "Pool path" %}:</dt>
                                    <dd class="col-9">{% if not path %}{% trans "None" %}{% else %}{{ path }}{% endif %}</dd>
                                    <dt class="col-3">{% trans "Pool status" %}:</dt>
                                    <dd class="col-9">{% if not status %}{% trans "None" %}{% else %}{{ status }}{% endif %}</dd>

                                    <dt class="col-3">{% trans "Size" %}:</dt>
                                    <dd class="col-9">{{size|filesizeformat }}</dd>
                                    
                                    <dt class="col-3">{% trans "Usage" %} ({{ percent }}%):</dt>
                                    <dd class="col-9">{{ used|filesizeformat }}</dd>
                                    <dt class="col-3">{% trans "State" %}</dt>
                                    <dd class="col-9">
                                        <form action="" method="post" role="form" aria-label="Storage start/stop form">{% csrf_token %}
                                            {% if state == 0 %}
                                                <input type="submit" class="btn btn-sm btn-secondary" name="start" value="{% trans "Start" %}">
                                                <input type="submit" class="btn btn-sm btn-danger" name="delete" value="{% trans "Delete" %}"
                                                        onclick="return confirm('{% trans "Are you sure?" %}')">
                                            {% else %}
                                                <input type="submit" class="btn btn-sm btn-outline-success" name="stop" value="{% trans "Ture" %}"
                                                        onclick="return confirm('{% trans "Are you sure?" %}')">
                                            {% endif %}
                                        </form>
                                    </dd>
                                    <dt class="col-3">{% trans "Autostart" %}</dt>
                                    <dd class="col-9">
                                        <form action="" method="post" role="form" aria-label="Storage disable/enable autostart form">{% csrf_token %}
                                            {% if autostart == 0 %}
                                                <input type="submit" class="btn btn-sm btn-secondary" name="set_autostart"
                                                        value="{% trans "Enable" %}">
                                                {% else %}
                                                <input type="submit" class="btn btn-sm btn-outline-success" name="unset_autostart"
                                                        onclick="return confirm('{% trans "Are you sure?" %}')" value="{% trans "True" %}">
                                            {% endif %}
                                        </form>
                                    </dd>
                                </dl>
                            </div>

                            <div class="col-lg-8">
                                {% if state %}
                                        {% comment %} <p>
                                            {% include 'search_block.html' %}
                                            <h5 class="page-header">{% trans "Volumes" %}</h5>
                                        </p> {% endcomment %}
                                        {% if volumes  %}
                                            <table id="stindexTable" class="table table-hover sortable-theme-bootstrap" data-sortable>
                                                <thead>
                                                <tr>
                                                    <th scope="col">#</th>
                                                    <th scope="col">{% trans "Name" %}</th>
                                                    <th scope="col">{% trans "Allocated" %}</th>
                                                    <th scope="col">{% trans "Size" %}</th>
                                                    <th scope="col">{% trans "Format" %}</th>
                                                    {% comment %} <th scope="colgroup" data-sortable="false" colspan="2">{% trans "Action" %}</th> {% endcomment %}
                                                    <th scope="col">{% trans "Action" %}</th>
                                                    <th scope="col"></th>
                                                </tr>
                                                </thead>
                                                <tbody class="searchable">
                                                {% for volume in volumes %}
                                                <tr>
                                                    <td>{{ forloop.counter }}</td>
                                                    <td>{{ volume.name|truncatechars:55 }}</td>
                                                    <td>{{ volume.allocation|filesizeformat }}</td>
                                                    <td>{{ volume.size|filesizeformat }}</td>
                                                    <td>{{ volume.type }}</td>
                                                    <td>
                                                        <!-- Modal Clone -->
                                                        <div class="modal fade" id="Clone{{ forloop.counter }}" tabindex="-1" role="dialog"
                                                                aria-labelledby="addHostLabel" aria-hidden="true">
                                                            <div class="modal-dialog">
                                                                <div class="modal-content">
                                                                    <div class="modal-header">
                                                                        <h5 class="modal-title">{% trans "Clone image" %} <span class="text-danger">{{ volume.name }}</span></h5>
                                                                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                                                                    </div>
                                                                    <form method="post" role="form" aria-label="Clone image file form">{% csrf_token %}
                                                                        <div class="modal-body">
                                                                            <div class="row">
                                                                                <label class="col-sm-3 col-form-label">{% trans "Name" %}</label>
                                                                                <div class="col-sm-6">
                                                                                    <input type="text" class="form-control" name="name" placeholder="{% trans "Name" %}" required pattern="[a-zA-Z0-9\.\-_]+">
                                                                                    <input type="hidden" name="image" value="{{ volume.name }}">
                                                                                </div>
                                                                            </div>
                                                                            <div class="row" id="image_format">
                                                                                <label class="col-sm-3 col-form-label">{% trans "Convert" %}</label>
                                                                                <div class="col-sm-6">
                                                                                    <input class="volume-convert" type="checkbox" name="convert" value="true">
                                                                                </div>
                                                                            </div>
                                                                            <div class="row format-convert">
                                                                                <label class="col-sm-3 col-form-label">{% trans "Format" %}</label>
                                                                                <div class="col-sm-6">
                                                                                    <select name="format" class="form-select image-format">
                                                                                        <option value="raw">{% trans "raw" %}</option>
                                                                                        <option value="qcow">{% trans "qcow" %}</option>
                                                                                        <option value="qcow2">{% trans "qcow2" %}</option>
                                                                                    </select>
                                                                                </div>
                                                                            </div>
                                                                            <div class="row meta-prealloc" style="display: none;">
                                                                                <label class="col-sm-3 col-form-label">{% trans "Metadata" %}</label>
                                                                                <div class="col-sm-6">
                                                                                    <input type="checkbox" name="meta_prealloc" value="true">
                                                                                </div>
                                                                            </div>
                                                                        </div>
                                                                        <div class="modal-footer">
                                                                            <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">{% trans "Close" %}</button>
                                                                            <button type="submit" class="btn btn-primary" name="cln_volume" onclick="showPleaseWaitDialog()">{% trans "Clone" %}</button>
                                                                        </div>
                                                                    </form>
                                                                </div> <!-- /.modal-content -->
                                                            </div> <!-- /.modal-dialog -->
                                                        </div> <!-- /.modal -->
                                                        {% if volume.type == "iso" %}
                                                            <button class="btn btn-sm btn-secondary" data-bs-toggle="modal" data-bs-target="#Clone{{ forloop.counter }}" title="{% trans "Clone" %}">
                                                                <i class="fa fa-files-o"></i>
                                                            </button>
                                                        {% else %}
                                                            <button class="btn btn-sm btn-secondary disabled">
                                                                <i class="fa fa-files-o"></i>
                                                            </button>
                                                        {% endif %}
                                                    </td>
                                                    <td>
                                                        <form action="" method="post" role="form" aria-label="Delete volume form">{% csrf_token %}
                                                            <input type="hidden" name="volname" value="{{ volume.name }}">
                                                            <button type="submit" class="btn btn-sm btn-secondary" name="del_volume" title="{% trans "Delete" %}" onclick="return confirm('{% trans "Are you sure?" %}')">
                                                                <i class="fa fa-trash"></i>
                                                            </button>
                                                        </form>
                                                    </td>
                                                </tr>
                                                {% endfor %}
                                                </tbody>
                                            </table>
                                        {% else %}
                                            <div class="col-lg-12">
                                                <div class="alert alert-warning shadow-sm">
                                                    <i class="fa fa-exclamation-triangle"></i> <strong>{% trans "Warning" %}:</strong> {% trans "Hypervisor doesn't have any Volumes" %}
                                                </div>
                                            </div>
                                        {% endif %}
                                    {% endif %}
                            </div>
                        </div>
                    </p>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock content %}
{% block script %}
    <link href="{% static 'css/jquery.dataTables.min.css' %}" rel="stylesheet">
    <script type="text/javascript" src="{% static 'js/jquery.dataTables.min.js' %}"></script>
    {% comment %} <script type="text/javascript" src="{% static 'js/dataTables.bootstrap.min.js' %}"></script> {% endcomment %}
    <script>
        $('#stindexTable').DataTable({
            autoWidth:true,////不开启自动宽度，用bootstrap的自适应去调整
            "lengthMenu": [5, 10, 20, 50],//表格行数选择框内数目 显示2条,4条,20条,50条
            "displayLength": 5,//默认的显示行数 (也就是每页显示几条数据)
            "order": [],
            "language": {//自定义语言提示
                "processing": "处理中...",
                "lengthMenu": "显示 _MENU_ 项结果",
                "zeroRecords": "没有找到相应的结果",
                "info": "第 _START_ 至 _END_ 行，共 _TOTAL_ 行",
                "infoEmpty": "第 0 至 0 项结果，共 0 项",
                "infoFiltered": "(由 _MAX_ 项结果过滤)",
                "search": "搜索",
                "infoPostFix": "",
                "url": "",
                "thousands": "'",
                "emptyTable": "表中数据为空",
                "loadingRecords": "载入中...",
                "infoThousands": ",",
                "paginate": {
                    "first": "首页",
                    "previous": "上页",
                    "next": "下页",
                    "last": "末页"
                }
            }
        });
    </script>
{% endblock script %}